<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      canvas {
        border: 1px solid #000;
        margin: 0 auto;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="282" height="500"></canvas>
    <script>
      const myCanvas = document.querySelector("#myCanvas");
      const context = myCanvas.getContext("2d");

      const img = new Image();
      img.src = "./飞机大战bg.jpeg";
      let index = 1;
      img.onload = function () {
        animate();
      };
      function animate() {
        context.save();
        context.clearRect(0, 0, myCanvas.width, myCanvas.height);
        context.translate(0, -index);

        context.drawImage(img, 0, 0);
        context.drawImage(img, 0, myCanvas.height);

        index++;
        if (index > myCanvas.height) {
          index = 1;
        }
        context.restore();
        requestAnimationFrame(animate);
      }
    </script>
  </body>
</html>
